Page({
    data: {
        list: null
    },
    onLoad() {
      let baseUrl = 'https://xiongmaoyin.com'
        wx.request({
            url: 'https://xiongmaoyin.com/api/favorlist',
            method: 'POST',
            header: {
                Authorization: 'Bearer' + wx.getStorageSync('token')
            },
            success: (res) => {
                if(res.data.code == 200) {
                    let list = res.data.data
                    for(let i in list) {
                        list[i].cover = baseUrl + list[i].cover
                        let price = list[i].price
                        list[i].price = Number(list[i].price)
                        if(isNaN(list[i].price)) {
                            list[i].price = Number(price.split('-')[0])
                        }
                    }
                    this.setData({
                        list: list
                    })
                }
            }
        })
        
    },
    handleClothes(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/detail/detail?id=' + id
        })
    },
    handleDelete(e) {
        let list = this.data.list, _list = []
        let id = e.currentTarget.dataset.id
        wx.showModal({
            title: '提示',
            content: '确认删除吗？',
            success: (res) => {
                if(res.confirm) {
                    wx.request({
                        url: 'https://xiongmaoyin.com/api/favor',
                        method: 'POST',
                        data: {
                            clothes: id
                        },
                        header: {
                           Authorization: 'Bearer' + wx.getStorageSync('token')
                        },
                        success: (res) => {
                            if(res.data.code == 200) {
                                for (let i = 0; i < list.length; i++) {
                                    if (list[i].id != id) {
                                        _list.push(list[i])
                                    }
                                }
                                this.setData({
                                    list: _list
                                })
                            }
                        }
                    })
                    
                    
                }
            }
        })
        
    }
})